﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-15>
//创建作者：<朱大鹏，zhudapeng@shanghai3h.com>
//modify by guodengjie at 20171012
//功能说明：提取考核表数据
//********************************************************************************//
using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;
using System.Collections.Generic;

namespace Web.Pages.YongShui._4.ChaoBiaoKZ
{
    public partial class TiQuKHBSJ : BasePage
    {
        #region 属性
        #endregion

        #region  变量
        /// <summary>
        /// 管理块BLL
        /// </summary>
        YS_JX_GUANLIKBLL glkbll = new YS_JX_GUANLIKBLL();
        SpHelper _spHelper = new SpHelper();
        #endregion

        #region 事件
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }
        /// <summary>
        /// 提取考核表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnKHB_Click(object sender, EventArgs e)
        {
            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            DataSet ds = _spHelper.P_CB_KAOHEBSJTQRZ(yd, DrpQK.Value.ToString());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                PageTools.ConfirmMsg(this, "本月已提取过，要再次提取吗？", "BtnConfirmKHB");
            }
            else
            {
                TQKHB(yd);
            }
        }
        protected void BtnConfirmKHB_Click(object sender, EventArgs e)
        {
           //if (Convert.ToBoolean(this.confirmValue.Value))
            {
               string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
                TQKHB(yd);
            }
        }
        protected void BtnConfirmHGB_Click(object sender, EventArgs e)
        {
            
        }
        /// <summary>
        /// 提取回灌表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnHGB_Click(object sender, EventArgs e)
        {
            string msg = "提取失败！";
            //_spHelper.P_YSCB_TQHGBSJ(0, 0, 0, out msg);
            _spHelper.P_YSCB_TQHGBSJ(BusinessTool.GetWaterYM(), 0, 0, out msg);
            PageTools.AlertMsg(this, msg);
        }
        #endregion

        #region  方法
        private void TQKHB(string ym)
        {
            string yd = ym;
            int? ErrCode = 0;
            string ErrMsg = "";
            string qk = DrpQK.Value.ToString();
            string msg = "";
            //if (qk == "0")
            //{
            //    foreach (DevExpress.Web.ListEditItem li in DrpQK.Items)
            //    {
            //        if (li.Value.ToString() != "0")
            //        {
            //            DataSet ds = _spHelper.P_YS_CB_TIQUKHSJ(li.Value.ToString(), Convert.ToInt32(yd.Substring(0, 4)), Convert.ToInt32(yd.Substring(4, 2)), out ErrCode, out ErrMsg, LoginInfo.CurrentUser.Account);
            //            msg += li.Text + "：" + ErrMsg + "\r\n";
            //        }
            //    }
            //}
            //else
            {
                DataSet ds = _spHelper.P_YS_CB_TIQUKHSJ(DrpQK.Value.ToString(), Convert.ToInt32(yd.Substring(0, 4)), Convert.ToInt32(yd.Substring(4, 2)), out ErrCode, out ErrMsg, LoginInfo.CurrentUser.UserID.ToString());

                //msg += DrpQK.Text + "：" + ErrMsg + "\r\n";
            }
            txtZhuanHuanJG.Text = ErrMsg.Replace("。","\r\n");
        }
        private string TQKHB(string ym, string glk)
        {
            string yd = ym;
            int? ErrCode = 0;
            string ErrMsg = "";
            string qk = glk;
            DataSet ds = _spHelper.P_YS_CB_TIQUKHSJ(glk, Convert.ToInt32(yd.Substring(0, 4)), Convert.ToInt32(yd.Substring(4, 2)), out ErrCode, out ErrMsg, LoginInfo.CurrentUser.UserID.ToString());
            return ErrMsg.Replace("。", "\r\n");
        }
        private void InitControl()
        {
            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            IYIM.Text = string.Format("{0}年{1}月", yd.Substring(0, 4), yd.Substring(4, 2));
            _spHelper.P_YS_CB_ZILAISGSTQRZ_ADD(Convert.ToInt32(yd));
            //获取当前用户站点信息
            //DataTable dtZhandian = BusinessTool.GetTextAndValue("view_as_userinfo", "ZHANDIANMC", "ZHANDIANBH", null, string.Format("UserId='{0}'", LoginInfo.CurrentUser.UserID)).Tables[0];
            //if (dtZhandian != null && dtZhandian.Rows.Count > 0)
            //{
            //    zhandianBH = dtZhandian.Rows[0]["VALUE"].ToString();
            //}
            string zhandianBH = BusinessTool.ZHANDIANBH();
            DataTable glkdt = glkbll.SelectYS_JX_GUANLIKsDynamic(" I_JLZT=0 and (S_ZHANDIAN='" + zhandianBH + "' or '00'='" + zhandianBH + "')", " S_GUANLIKH");

            DrpQK.DataSource = glkdt;
            DrpQK.TextField = "S_GUANLIKMC";
            DrpQK.ValueField = "S_GUANLIKH";
            DrpQK.DataBind();
            if (zhandianBH == "00")
            {
                DrpQK.Items.Insert(0, new DevExpress.Web.ListEditItem("所有区块", "0"));
            }
            if (glkdt != null && glkdt.Rows.Count > 0)
            {
                string ids = "";
                foreach (DataRow dr in glkdt.Rows)
                {
                    ids += dr["S_GUANLIKH"] + ",";
                }
                glklist.Value = ids;
            }
            DrpQK.SelectedIndex = 0;
        }
        #endregion

        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            if (e.Parameter == "check")
            {
               
                string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
                DataSet ds = _spHelper.P_CB_KAOHEBSJTQRZ(yd, DrpQK.Value.ToString());
                int code =0;
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    code = 1;
                }
                e.Result = e.Parameter + "|" + code + "|";
            }
            if (e.Parameter == "tiqu")
            {
                int code = -1;
                string msg = "";
                string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
                string newids = "";
                if (HiddenList.Contains("glk"))
                {
                    string[] glksT = HiddenList.Get("glk").ToString().Split(new string[]{","},StringSplitOptions.RemoveEmptyEntries);                    
                    List<string> glks = new List<string>(glksT);
                    string glk = glks[0];
                    msg = TQKHB(yd, glk);
                    glks.RemoveAt(0);
                    newids = string.Join(",", glks);
                    code = 1;
                    //所有管理块提取结束对抄表日期进行处理
                    if (glks.Count==0)
                    {
                        int? rsno = -1;
                        string rsmsg = "";
                        this._spHelper.P_YSCB_CHAOBIAORQCL(out rsno, out rsmsg);
                    }
                    /*
                    string glk = HiddenList.Get("glk").ToString().Split(',')[0];
                    string[] ids = HiddenList.Get("glk").ToString().Split(',');
                    
                    if (glk.Trim().Length > 0)
                    {
                        foreach (string s in ids)
                        {
                            if (s.Trim() != glk.Trim())
                            {
                                newids += s + ",";
                            }
                        }
                        msg = TQKHB(yd, glk);
                        code = 1;
                    }
                    else
                    {
                        code = 2;
                    }
                     * */
                }
                e.Result = e.Parameter + "|" + code + "|"+msg+"|"+newids;
            }
        }
    }
}